<template>
    <div class="banner-container">
        <el-table
            :data="banner"
            style="width: 100%;"
            border
        >
            <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
            <el-table-column prop="title" label="标题" width="150" align="center"></el-table-column>
            <el-table-column prop="description" label="描述" align="center"></el-table-column>
            <el-table-column label="中图" width="180" align="center">
                <template slot-scope="scope">
                    <el-image
                        style="width: 100px;"
                        :src="scope.row.midImg"
                        fit="fill"
                    ></el-image>
                </template>
            </el-table-column>
            <el-table-column label="大图" width="180" align="center">
                <template slot-scope="scope">
                    <el-image
                        style="width: 100px;"
                        :src="scope.row.bigImg"
                        fit="fill"
                    ></el-image>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="100" align="center">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="editBanner(scope.row)"></el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog
            title="编辑"
            :visible.sync="dialogVisible"
            width="60%"
            :before-close="handleClose"
        >
            <el-form :mode="dialogForm">
                <el-form-item label="标题" :label-width="formLabelWith">
                    <el-input v-model="dialogForm.title"></el-input>
                </el-form-item>
                <el-form-item label="描述" :label-width="formLabelWith">
                    <el-input v-model="dialogForm.description"></el-input>
                </el-form-item>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="中图" :label-width="formLabelWith">
                            <Upload v-model="dialogForm.midImg"></Upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="大图" :label-width="formLabelWith">
                            <Upload v-model="dialogForm.bigImg"></Upload>
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="setBannerConfirm">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getBanner,setBanner } from '@/api/banner'
import { server_URL } from '@/urlConfig'
import Upload from '@/components/Upload.vue'

export default {
    name: 'Banner',
    components: {
        Upload
    },
    data() {
        return {
            banner: [],
            dialogVisible: false,
            formLabelWith: '100px',
            dialogForm: {
                title: '',
                description: '',
                midImg: '',
                bigImg: '',
                id: ''
            },
        }
    },
    created() {
        this.fetchData()
    },
    methods: {
        fetchData() {
            getBanner().then(res => {
                this.banner = res.data
                for(var item of this.banner){
                    item.midImg2 = server_URL + item.midImg
                    item.bigImg2 = server_URL + item.bigImg
                }
            })
        },
        editBanner(row) {
            this.dialogForm = { ...row }
            this.dialogVisible = true
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done()
                })
                .catch(_ => {})
        },
        setBannerConfirm() {
            let arr = [...this.banner];
            for(var i = 0;i<arr.length;i++){
                if(arr[i].id == this.dialogForm.id){
                    arr[i] = this.dialogForm
                }
            }
            setBanner(arr).then(res => {
                this.dialogVisible = false
                this.$message({
                    message: '修改成功',
                    type: 'success'
                })
                this.fetchData()
            })

            // this.dialogVisible = false
        }
    }
}
</script>

<style scoped lang="scss">
.banner-container {
    padding: 20px;
}
</style>
